package com.example.demo.poc.executor;

import com.example.demo.poc.domain.ConfigPropItem;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ContextProcessor {
    private DocumentContext _ctx;


    public ContextProcessor(String json) {
        read(json);
    }

    public ContextProcessor(File json, String charSet) throws IOException {
        read(FileUtils.readFileToString(json, charSet));
    }

    public void importContextSource(String json) {
        read(json);
    }


    private void read(String json) {
        json = unescapeToValidJsonString(json);
        if (null == json) {
            return;
        }

        this._ctx = JsonPath.parse(json);

    }

    public Object getPathValue(String jsonPath) {
        if(StringUtils.isBlank(jsonPath))
            return null;
        return this._ctx.read(jsonPath);
    }

    public List<Object> getPathValueList(String jsonPath) {
        if(StringUtils.isBlank(jsonPath)){
            return null;
        }
        Object o = getPathValue(jsonPath);
        if(o instanceof JSONArray){
            return parseJsonListValue((JSONArray) o);
        }

        return parseJsonListValue(o);
    }

    private List<Object> parseJsonListValue(JSONArray jsonArray) {
//        if (jsonArray != null & jsonArray.size() > 0) {
            List<Object> list = new ArrayList(jsonArray.size());
            for (int i = 0; i < jsonArray.size(); i++) {
                Object o = jsonArray.get(i);
                list.add(o);
            }
            return list;
//        }
    }

    private List<Object> parseJsonListValue(LinkedHashMap map){
        List<Object> list = new ArrayList();
        list.add(map);
        return list;
    }

    private List<Object> parseJsonListValue(Object obj){
        List<Object> list = new ArrayList();
        list.add(obj);
        return list;
    }


    private List<Map<String, Object>> parseJsonPairValue(LinkedHashMap<String, Object> map, String targetKeyName, String targetValueName) {

        return null;
    }

    private Object parseValue(Object obj) {

        return null;
    }

    public <T> T getPathValue(String jsonPath, Class<T> type) {
        String typeName = type.getTypeName();

//        System.out.println(typeName);

        return null;
    }


    public static void main(String[] args) {
//        ContextProcessor proc = new ContextProcessor("");
//        proc.getPathValue("",String.class);

        Class t = String.class;
        System.out.println(t.getSimpleName());
        System.out.println(t.getCanonicalName());

    }


    private String unescapeToValidJsonString(String json) {
        if (StringUtils.isAnyBlank(json)) {
            return null;
        }

        json = StringEscapeUtils.unescapeJson(json).replaceAll("\"\\{", "{").replaceAll("}\"", "}");

        return json;
    }
}
